package com.nuo.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ClassUtil {
	
	String test = "123";
	
	
	/**
	 * 为obj对象添加一个属性名为name的值
	 * @param obj
	 * @param name
	 * @param value
	 */
	public static void setBeanValue(Object obj, String name, String value){
		
		Class c = obj.getClass();
		try{
			
			Field[] fields = c.getDeclaredFields();
			Field goal = null;
			for (Field field : fields) {
				if(field.getName().equals(name)){
					goal = field;
					break;
				}
			}
			
			if(goal == null){
				return;
			}
			
			String methodName = "set"+name.substring(0, 1).toUpperCase() + name.substring(1);
			Method m = c.getMethod(methodName, new Class[]{goal.getClass()});		
			m.invoke(obj, value);
			
		}catch(Exception ex){
			throw new RuntimeException("��ȡ����ʱ����.",ex);
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	public static void main(String[] args) {
		Class c = ClassUtil.class;

			
		Field[] fields = c.getDeclaredFields();
		Field goal = null;
		for (Field field : fields) {
			if(field.getName().equals("test")){
				goal = field;
				break;
			}
		}
		
		System.out.println(goal.getType());
		
	}
	
	
}
